package cn.signalpha.modules.social.controller.api;

import cn.signalpha.common.annotation.FormRepeatCommitAnnotation;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.social.dto.FansDTO;
import cn.signalpha.modules.social.entity.Fans;
import cn.signalpha.modules.social.service.FansService;
import cn.signalpha.modules.social.vo.FansVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_NAME_PREFIX + "social/wap/fans")
@Api(value = "粉丝", tags = "粉丝")
public class ApiFansController extends BaseController {

	private final UserService userService;
	private final FansService fansService;
	private final ApplicationEventPublisher eventPublisher;

    @GetMapping("/my/focus/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "我的关注", notes = "")
	public R<IPage<FansVO>> myFocusPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("fansId", getUser().getUserId().toString());
		return R.data(fansService.selectFansPage(Condition.getPage(query), params));
    }

	@GetMapping("/my/fans/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "我的粉丝", notes = "")
	public R<IPage<FansVO>> myFansPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId().toString());
		return R.data(fansService.selectFansPage(Condition.getPage(query), params));
	}

	@PostMapping("/save")
	@FormRepeatCommitAnnotation
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "关注", notes = "")
	public R save(@Valid @RequestBody FansDTO fansDTO) {
		Long userId = getUser().getUserId();
		User user = userService.getById(fansDTO.getUserId());
		Assert.isNull(user, MsgConstant.ACCOUNT_NULL);
		Assert.isFalse(fansDTO.getUserId().equals(userId), "不可自己关注自己");

		Fans fans =  fansService.getByUserIdAndFansId(fansDTO.getUserId(), userId);
		if(ObjectUtil.isEmpty(fans)){
			fans = Fans.builder().userId(fansDTO.getUserId()).fansId(userId).build();
			fansService.save(fans);
		}
//		eventPublisher.publishEvent(new NoticeEvent(3, fansDTO.getUserId(), fans.getId(), NoticeEntityTypeEnum.FANS.getKey()));
		return R.status(true);
	}

	@PostMapping("/remove")
	@FormRepeatCommitAnnotation
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "取消关注", notes = "")
	public R remove(@Valid @RequestBody FansDTO fansDTO) {
		return R.status(fansService.remove(Wrappers.<Fans>lambdaQuery().eq(Fans::getUserId, fansDTO.getUserId()).eq(Fans::getFansId, getUser().getUserId())));
	}

}
